﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace GameStateManagement
{
    public class CheckpointManager : Microsoft.Xna.Framework.GameComponent
    {
        public static float MinRadiusMagnitude = 0.4f;
        public static float MaxRadiusMagnitude = 0.9f;
        public static float CheckpointExpandRadius = 300f;
        public static float CheckpointSpeedMultiplier = 0.05f;   // Multiplier to determine speed of next checkpoint (multiplied by number of checkpoints passed)
        public static float CheckpointMaxSpeed = 0.8f;
        private static CheckpointManager checkpointManager = null;

        private Random rand;

        public static List<Checkpoint> checkpoints = new List<Checkpoint>();

        private static int m_iCheckpointsPassed = 0;

        private CheckpointManager(Game game): base(game)
        {
            rand = new Random();
        }
       
        public override void  Initialize()
        {
            CreateCheckpoint(0f);
            base.Initialize();
        }

        public static CheckpointManager InitializeCheckpointManager(Game game)
        {
            if (checkpointManager == null)
            {
                checkpointManager = new CheckpointManager(game);
                game.Components.Add(checkpointManager);
            }
            return checkpointManager;
        }

        public static void DestroyCheckpointManager(Game game)
        {
            // Delete all the wormholes.
            foreach (Checkpoint checkpoint in checkpoints)
            {
                game.Components.Remove(checkpoint);
            }
            checkpoints.Clear();

            // Remove from game.
            game.Components.Remove(checkpointManager);

            m_iCheckpointsPassed = 0;

            // Drop the reference.
            checkpointManager = null;
        }

        public static CheckpointManager GetCheckpointManager()
        {
            return checkpointManager;
        }

        public Checkpoint GetCurrentCheckpoint()
        {
            if (checkpoints.Count == 0)
            {
                return null;
            }
            return checkpoints[0];
        }

        public void CheckpointPassed(Checkpoint checkpoint)
        {
            //Expand Stars
            ShrinkingStarSphere.GetShrinkingStarSphere().Expand(CheckpointManager.CheckpointExpandRadius);

            //Delete Old CHeckpoint
            DeleteCheckpoint(checkpoint);

            m_iCheckpointsPassed++;

            //Create Checkpoint
            CreateCheckpoint((float)m_iCheckpointsPassed * CheckpointManager.CheckpointSpeedMultiplier);
        }

        public void CreateCheckpoint(float speed)
        {
            // Clamp the speed.
            if (speed > CheckpointManager.CheckpointMaxSpeed) speed = CheckpointManager.CheckpointMaxSpeed;

            // Generate float between MinRadiusMagnitude and MaxRadiusMagnitude.
            float magnitudeRatio = CheckpointManager.MinRadiusMagnitude + ((float)rand.NextDouble() * (CheckpointManager.MaxRadiusMagnitude - CheckpointManager.MinRadiusMagnitude));
            
            Checkpoint checkpoint = new Checkpoint(Game, magnitudeRatio, speed);
            checkpoints.Add(checkpoint);
            Game.Components.Add(checkpoint);
            
        }
        public override void Update(GameTime gameTime)
        {   
            base.Update(gameTime);
        }

        public void DeleteCheckpoint(Checkpoint checkpoint)
        {
            Game.Components.Remove(checkpoint);
            checkpoints.Remove(checkpoint);
        }
    }
}
